﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace schedule
{
    public partial class MakeForm : Form
    {
        private FriendList m_friendlist = new FriendList();
        private int m_invateListCount = 0;
        private int m_maxLengthName = 0;
        private bool m_makeEnable = true;
        private WorkInfo m_currentWorkInfo = null;
        private bool m_fromSearch = false;
        public MakeForm(bool fromSearch)
        {
            InitializeComponent();
            checkBox2.Checked = true;
            groupBox1.Enabled = false;

            pictureBox1.Image = global::schedule.Properties.Resources.circle;
            pictureBox2.Image = global::schedule.Properties.Resources.question;
            pictureBox3.Image = global::schedule.Properties.Resources.x;

            m_fromSearch = fromSearch;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            checkBox2.Checked = !checkBox1.Checked;
            groupBox1.Enabled = checkBox1.Checked;            
        }

        // 메인 달력을 통해 새로 만들기를 할 때, 시간 기본 설정
        public void setTime(DateTime dateTime)
        {
            dtpStart.Value = dateTime;
            dtpEnd.Value = dateTime;
        }

        // 수정 버튼을 통해 진입시. 폼의 항목들을 채워주자.
        // 폼이 모두 끝난뒤 데이터 저장은... WorkManager::changeWorkInfo 부분에서 한다.
        public void editContent(WorkInfo wi)
        {
            WorkManager wm = WorkManager.Instance();
            wm.changeMakeForm(wi, this);

            if (wm.isMadeByMe(wi) == false )
            {
                m_makeEnable = false;
                //this.Enabled = false;
                groupBox1.Enabled = false;
                checkBox1.Enabled = false;
                checkBox2.Enabled = false;
                tbTitle.Enabled = false;
                dtpStart.Enabled = false;
                dtpEnd.Enabled = false;
                tbPlace.Enabled = false;
                tbSummary.Enabled = false;

                btnOK.Text = "참가";

                if (wm.isMyGroup(wi) == true)
                {
                    btnOK.Text = "확인";
                    btnCancel.Visible = false;
                }

            }

            m_currentWorkInfo = wi;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            m_friendlist.ShowDialog();

            List<string> names = m_friendlist.getInvateFriends();
            int index = 0;

            foreach (string name in names)
            {
                if (m_maxLengthName < name.Length)
                {
                    m_maxLengthName = name.Length;
                }
            }
            
            foreach (string name in names)
            {
                bool add = true;
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if (row.Cells[0].EditedFormattedValue.ToString() == name)
                    {
                        add = false;
                        break;
                    }
                }
                
                if (add == true)
                {
                    if (m_invateListCount == 0)
                    {
                        dataGridView1.Rows[0].Cells[0].Value = name;
                        dataGridView1.Rows[0].Cells[1].Value = global::schedule.Properties.Resources.circle;
                        dataGridView1.Rows[0].Cells[2].Value = global::schedule.Properties.Resources.question;

                        dataGridView1.Rows[0].Height = 32;
                        dataGridView1.Columns[0].Width = m_maxLengthName * 8;
                        dataGridView1.Size = new Size(dataGridView1.Size.Width, dataGridView1.Size.Height + 32);
                        m_invateListCount++;
                    }
                    else
                    {
                        int i = dataGridView1.Rows.Add(name, global::schedule.Properties.Resources.circle, global::schedule.Properties.Resources.question);
                        dataGridView1.Rows[i].Height = 32;

                        dataGridView1.Columns[0].Width = m_maxLengthName * 8;
                        if (dataGridView1.Size.Height < 115-32)
                            dataGridView1.Size = new Size(dataGridView1.Size.Width, dataGridView1.Size.Height + 32);
                        m_invateListCount++;
                    }
                }
            }

            int totalLen = 0;
            foreach (DataGridViewColumn column in dataGridView1.Columns)
            {
                totalLen += column.Width;
            }
            if (totalLen > dataGridView1.Width)
            {
                dataGridView1.Width = totalLen + 3;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Category1 f = new Category1();
            f.ShowDialog();
            if (f.getCateg1Selected())
                button1.Text = f.getCategoryName();
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            checkBox1.Checked = !checkBox2.Checked;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            bool add = true;
            string name = textBox4.Text;
            if (name.Length <= 0)
                return;

            if (m_maxLengthName < name.Length)
            {
                m_maxLengthName = name.Length;
            }

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Cells[0].EditedFormattedValue.ToString() == name)
                {
                    add = false;
                    break;
                }
            }

            if (add == true)
            {
                if (m_invateListCount == 0)
                {
                    dataGridView1.Rows[0].Cells[0].Value = name;
                    dataGridView1.Rows[0].Cells[1].Value = global::schedule.Properties.Resources.circle;
                    dataGridView1.Rows[0].Cells[2].Value = global::schedule.Properties.Resources.question;

                    dataGridView1.Rows[0].Height = 32;
                    dataGridView1.Columns[0].Width = m_maxLengthName * 8;
                    dataGridView1.Size = new Size(dataGridView1.Size.Width, dataGridView1.Size.Height + 32);
                }
                else
                {
                    int i = dataGridView1.Rows.Add(name, global::schedule.Properties.Resources.circle, global::schedule.Properties.Resources.question);
                    dataGridView1.Rows[i].Height = 32;
                    dataGridView1.Columns[0].Width = m_maxLengthName * 8;

                    if (dataGridView1.Size.Height < 115 - 32)
                        dataGridView1.Size = new Size(dataGridView1.Size.Width, dataGridView1.Size.Height + 32);
                }

                m_invateListCount++;
                int totalLen = 0;
                foreach (DataGridViewColumn column in dataGridView1.Columns)
                {
                    totalLen+=column.Width;
                }
                if (totalLen > dataGridView1.Width)
                {
                    dataGridView1.Width = totalLen + 3;
                }
                
            }
        }

        public void addInvateList(string name)
        {
            if (m_invateListCount == 0)
            {
                dataGridView1.Rows[0].Cells[0].Value = name;
                dataGridView1.Rows[0].Cells[1].Value = global::schedule.Properties.Resources.circle;
                dataGridView1.Rows[0].Cells[2].Value = global::schedule.Properties.Resources.question;

                dataGridView1.Rows[0].Height = 32;
                dataGridView1.Columns[0].Width = m_maxLengthName * 8;
                dataGridView1.Size = new Size(dataGridView1.Size.Width, dataGridView1.Size.Height + 32);
            }
            else
            {
                int i = dataGridView1.Rows.Add(name, global::schedule.Properties.Resources.circle, global::schedule.Properties.Resources.question);
                dataGridView1.Rows[i].Height = 32;
                dataGridView1.Columns[0].Width = m_maxLengthName * 8;

                if (dataGridView1.Size.Height < 115 - 32)
                    dataGridView1.Size = new Size(dataGridView1.Size.Width, dataGridView1.Size.Height + 32);
            }

            m_invateListCount++;
            int totalLen = 0;
            foreach (DataGridViewColumn column in dataGridView1.Columns)
            {
                totalLen += column.Width;
            }
            if (totalLen > dataGridView1.Width)
            {
                dataGridView1.Width = totalLen + 3;
            }
        }

        public int getInvateListSize()
        {
            return m_invateListCount;
        }

        public string getInvateFriend(int index)
        {
            return dataGridView1.Rows[index].Cells[0].Value as string;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (btnOK.Text == "참가")
            {
                if (m_currentWorkInfo != null)
                {
                    m_currentWorkInfo.isJoin = true;
                }
            }
        }
    }
}
